package org.bp.spider.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

/**
 * @author baiping
 * @version 1.0
 * @date 2017/9/6
 */
public class StringUtils {
   // private static Log log = LogFactory.getLog(StringUtils.class);
    public static boolean isAbsEmpty(String str) {
        return str == null?true:str.trim().isEmpty();
    }

    public static boolean isNotAbsEmpty(String str) {
        return !isAbsEmpty(str);
    }

    public static String getUUID(){
        return UUID.randomUUID().toString().replace("-","");
    }

    public static String append(String source,String ...strs){
        StringBuilder stringBuilder = new StringBuilder(source);
        if(CollectionsUtils.isNotAbsEmpty(strs)){
            for(String str : strs){
                stringBuilder.append(str);
            }
        }
        return stringBuilder.toString();
    }

    public static <T> void visitArray(T[] arr){
        for(T t : arr){
            System.out.println(t);
        }
    }



    public static <T> String listToString(List<T> list, String item_split) {
        if(CollectionsUtils.isAbsEmpty(list)) {
            return null;
        } else {
            StringBuilder str = new StringBuilder();
            Iterator<T> var3 = list.iterator();

            while(var3.hasNext()) {
                Object item = var3.next();
                str.append(item.toString()).append(item_split);
            }

            if(str.length() > 0) {
                str.deleteCharAt(str.length() - 1);
            }

            return str.toString();
        }
    }
    public static String getExtName(String fileName){
        int pos = fileName.lastIndexOf(46);
        return pos==-1?fileName:fileName.substring(pos,fileName.length());
    }
    public static String delExtName(String fileName){
        int pos = fileName.lastIndexOf(46);
        return pos==-1?fileName:fileName.substring(0,pos);
    }

    public static String linuxPath(String path){
        return path.replace("\\","/");
    }

    public static String[] splitWorker(String str, String separatorChars) {
        int max = -1;
        boolean preserveAllTokens =false;
        if(str == null) {
            return null;
        } else {
            int len = str.length();
            if(len == 0) {
                return new String[0];
            } else {
                ArrayList<String> list = new ArrayList<>();
                int sizePlus1 = 1;
                int i = 0;
                int start = 0;
                boolean match = false;
                boolean lastMatch = false;
                if(separatorChars != null) {
                    if(separatorChars.length() != 1) {
                        label87:
                        while(true) {
                            while(true) {
                                if(i >= len) {
                                    break label87;
                                }

                                if(separatorChars.indexOf(str.charAt(i)) >= 0) {
                                    if(match || preserveAllTokens) {
                                        lastMatch = true;
                                        if(sizePlus1++ == max) {
                                            i = len;
                                            lastMatch = false;
                                        }

                                        list.add(str.substring(start, i));
                                        match = false;
                                    }

                                    ++i;
                                    start = i;
                                } else {
                                    lastMatch = false;
                                    match = true;
                                    ++i;
                                }
                            }
                        }
                    } else {
                        char sep = separatorChars.charAt(0);

                        label71:
                        while(true) {
                            while(true) {
                                if(i >= len) {
                                    break label71;
                                }

                                if(str.charAt(i) == sep) {
                                    if(match || preserveAllTokens) {
                                        lastMatch = true;
                                        if(sizePlus1++ == max) {
                                            i = len;
                                            lastMatch = false;
                                        }

                                        list.add(str.substring(start, i));
                                        match = false;
                                    }

                                    ++i;
                                    start = i;
                                } else {
                                    lastMatch = false;
                                    match = true;
                                    ++i;
                                }
                            }
                        }
                    }
                } else {
                    label103:
                    while(true) {
                        while(true) {
                            if(i >= len) {
                                break label103;
                            }

                            if(Character.isWhitespace(str.charAt(i))) {
                                if(match || preserveAllTokens) {
                                    lastMatch = true;
                                    if(sizePlus1++ == max) {
                                        i = len;
                                        lastMatch = false;
                                    }

                                    list.add(str.substring(start, i));
                                    match = false;
                                }

                                ++i;
                                start = i;
                            } else {
                                lastMatch = false;
                                match = true;
                                ++i;
                            }
                        }
                    }
                }

                if(match || preserveAllTokens && lastMatch) {
                    list.add(str.substring(start, i));
                }

                return (String[])list.toArray(new String[list.size()]);
            }
        }
    }

}
